package dipl.visualizer.graphics.shape;

import dipl.algorithm.math.fp.primitive.Point2df;
import dipl.algorithm.math.fp.primitive.Vector2df;
import dipl.algorithm.math.fp.primitive.op.ComplexOps;
import dipl.algorithm.math.geometry.BoundingWedge2d;
import dipl.visualizer.graphics.PaintContext;
import java.awt.Color;
import java.awt.Font;

public class BoundingWedge2dShape extends AbstractShape {

	//
	// CONSTRUCTOR METHODS
	//
	
	/// <summary>
	/// Constructs shape of a 2d-bounding wedge at given origin.
	/// </summary>
	/// <param name="origin"></param>
	/// <param name="name"></param>
	/// <param name="wedge">the wedge</param>
	/// <param name="color">painting color</param>
	/// <param name="colorInterior">interior color</param>
	/// <param name="labels"></param>
	/// <param name="font"></param>
	public BoundingWedge2dShape(
		String name,
		Point2df origin,
		BoundingWedge2d wedge,
		Color color,
		Color colorInterior,
		boolean labels,
		Font font
	) {

		super( name, color );
		this.origin = new Point2df( origin.x, origin.y );
		this.wedge = wedge;
		this.left = new Vector2df( wedge.left.x.doubleValue(), wedge.left.y.doubleValue() );
    this.mleft = new Vector2df( -this.left.x, -this.left.y );
		this.angle = -wedge.angle.doubleValue()/(Math.PI/180.0);
		if( this.left.x != 0.0 || this.left.y != 0.0 ) {
			this.left.Normalize();
      this.mleft.Normalize();
			this.angleStart = ComplexOps.Argument( left.x, left.y )/(Math.PI/180.0);
			this.mangleStart = ComplexOps.Argument( mleft.x, mleft.y )/(Math.PI/180.0);
		}
		else {
			this.angleStart = 0.0;
      this.mangleStart = 0.0;
		}
		this.colorInterior = colorInterior;
		this.displayLabels = labels;
		this.font = font;
	}

	//
	// PUBLIC METHODS
	//
	
	@Override
	public void Paint( PaintContext pc ) {
		// fill cone
		pc.SetColor( colorInterior );
		pc.FillPie( origin, 1.0, 1.0, angleStart, angle );
		pc.FillPie( origin, 1.0, 1.0, mangleStart, angle );
		// draw bounding vectors
		pc.SetColor( color );
		pc.DrawLine( origin.x, origin.y, origin.x+left.x, origin.y+left.y );
		pc.DrawLine( origin.x, origin.y, origin.x+mleft.x, origin.y+mleft.y );
		// draw origin
		pc.DrawLine( origin.x-0.02, origin.y, origin.x+0.02, origin.y );
		pc.DrawLine( origin.x, origin.y+0.02, origin.x, origin.y-0.02 );
		// draw labels
		if( displayLabels ) {
			pc.SetFont( font );
			pc.DrawString( "0", origin.x+0.02, origin.y+0.02 );
			pc.DrawString( "l", origin.x+left.x+0.02, origin.y+left.y+0.02 );
			pc.DrawString( "-l", origin.x+mleft.x+0.02, origin.y+mleft.y+0.02 );
		}
	}

	//
	// PROPERTIES
	//

	public BoundingWedge2d Wedge() {
		return wedge;
	}

	@Override
	public Point2df PointOnShape() {
		return origin;
	}

	//
	// MEMBERS
	//

	/** whether or not to display labels */
	private boolean displayLabels;
	/** origin */
	private Point2df origin;
	/** (clockwise) left bounding vector */
	private Vector2df left;
	/** vector in opposite direction of (clockwise) left bounding vector */
	private Vector2df mleft;
	/** opening angle (in degree) */
	private double angle;
	/** angle between (1.0,0.0) and left (clockwise) (in degree) */
	private double angleStart;
	/** angle between (1.0,0.0) and opposite left (clockwise) (in degree) */
  private double mangleStart;
	/** the wedge */
	private BoundingWedge2d wedge;
	/** color for painting cone's interior */
	private Color colorInterior;
	/** text font */
	private Font font;
}
